<template>
	<view class="home">
		<uni-nav-bar shadow left-icon="left" title="修改模板" @clickLeft="back" />
		<view class="form_box">
			<!-- 基础表单校验 -->
			<uni-forms ref="form" :model="templateData" label-width="105px" label-align="right">
				<uni-section title="自定义参数" sub-title="天气只能精确至 区 县" subTitleFontSize="14px" subTitleColor="#ff0000"
					type="line">
					<view class="example">
						<uni-forms-item label="行政区划编码" required name="district" :rules="[{
									required: true,
									errorMessage: '地址码不能为空，即当地身份证前六位'
								},{
									minLength: 6,
									maxLength: 6,
									errorMessage: '请输入正确的区县地址码(身份证前六位)',
								}]">
							<uni-easyinput :disabled="!isValue" type="number" v-model="templateData.district"
								placeholder="输入区县行政区划编码换取天气信息" />
						</uni-forms-item>
						<uni-forms-item label="恋爱纪念日" required name="lianaitime" :rules="[{
									required: true,
									errorMessage: '恋爱纪念日不能为空'
								}]">
							<uni-datetime-picker :disabled="!isValue" type="date" :value="templateData.lianaitime" :end="startTime"
								@change="changeTime($event,1)" placeholder="请输入恋爱纪念日" />
						</uni-forms-item>
						<uni-forms-item label="生日" required name="birthday" :rules="[{
									required: true,
									errorMessage: '生日不能为空'
								}]">
							<uni-datetime-picker :disabled="!isValue" type="date" :value="templateData.birthday" :end="startTime"
								@change="changeTime($event,2)" placeholder="请输入生日" />
						</uni-forms-item>
						<uni-forms-item label="备注" required name="remark" :rules="[{
									required: true,
									errorMessage: '备注不能为空'
								}]">
							<uni-easyinput :disabled="!isValue" v-model="templateData.remark" placeholder="请输入备注" />
						</uni-forms-item>

						<uni-forms-item label="早上" required name="cronMoring" :rules="[{
									required: true,
									errorMessage: '早上时间(06:00-10:30)不能为空'
								}]">
							<view class="timer_box">
								<picker mode="time" :disabled="!isValue&&templateData.moringStatus===0" :value="templateData.cronMoring" start="06:00" end="10:30"
									@change="changeLog($event,1)">
									<uni-easyinput :disabled="!isValue&&templateData.moringStatus===0" :clearable="false"
										v-model="templateData.cronMoring" placeholder="请选择时间(06:00-10:30)" />
								</picker>
								<switch :disabled="!isValue" :checked="templateData.moringStatus===1" color="#00ff7f"
									style="transform:scale(0.7);padding-left: 1rem;" @change="changeSwitch($event,1)" />
								<!-- <button type="primary" border @click="timerMoring">保存</button> -->
							</view>
						</uni-forms-item>
						<uni-forms-item label="晚上" required name="cronNight" :rules="[{
									required: true,
									errorMessage: '晚上时间(17:00-00:30)不能为空'
								}]">
							<view class="timer_box">
								<picker mode="time" :disabled="!isValue&&templateData.nightStatus===0" :value="templateData.cronNight" start="17:00" end="23:59"
									@change="changeLog($event,2)">
									<uni-easyinput :disabled="!isValue&&templateData.nightStatus===0" :clearable="false"
										v-model="templateData.cronNight" placeholder="请选择时间(17:00-23:59)" />
								</picker>
								<switch :disabled="!isValue" :checked="templateData.nightStatus===1" color="#00ff7f"
									style="transform:scale(0.7);padding-left: 1rem;" @change="changeSwitch($event,2)" />
								<!-- <button type="primary" border @click="timerNight">保存</button> -->
							</view>
						</uni-forms-item>
						<view class="btn_box">
							<button :type="!isValue?'default':'primary'" border @click="updateTemplate()">{{!isValue?'暂无可修改模板，点击前往生成':'保存修改'}}</button>
						</view>
					</view>
				</uni-section>
<!-- 				<uni-section title="定时器修改" sub-title="点击滑块关闭定时推送" type="line">
					<view class="example">

					</view>
				</uni-section> -->

			</uni-forms>
<!-- 			<view class="" v-if="isValue">
			
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		setHasData
	} from '@/api/wxpush/index.js'
	import {
		getDate,
		getTime,
		formatCorn,
		formatTime
	} from '@/util/common.js'
	import {
		setTimer
	} from '@/api/job/index.js'
	import {
		setCronMoringStatus,
		setCronNightStatus
	} from '@/api/jobStatus/index.js'
	import {
		getTemplate
	} from '@/api/openid/index.js'
	export default {
		data() {
			return {
				isValue: false,
				userInfo: '',
				template: '',
				// 校验表单数据
				templateData: {
					appid: '',
					secret: '',
					openid: '',
					morningTemplateId: '',
					district: '',
					lianaitime: '',
					birthday: '',
					remark: '',
					beizhu: 'beizhu',
					shengri: 'shengri',
					jinju: 'jinju',
					caihongpi: 'caihongpi',
					lianai: 'lianai',
					tianqi: 'tianqi',
					riqi: 'riqi',
					low: 'low',
					high: 'high',
					single: '',
					cronMoring: '',
					cronNight: '',
					moringStatus: 0,
					nightStatus: 0,
				},
				isUpload: false,
				isTips: false,
				startTime: '',
				endTime: '',
				
			}
		},
		onLoad(option) {
			if (option.userInfo) {
				this.userInfo = JSON.parse(option.userInfo)
			} else {
				this.userInfo = uni.getStorageSync('userInfo')
			}
			this.startTime = getDate()
			this.endTime = getDate()
		},
		onShow() {
			this.templateData.appid = getApp().globalData.APP_ID
			this.templateData.secret = getApp().globalData.SECRET
			this.templateData.openid = this.userInfo ? this.userInfo.openid : ''
			this.getTemplate()
		},

		methods: {
			back() {
				let pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.reLaunch({
						url: "/pages/index/index"
					})
				}
			},
			// 初始化数据
			setData(obj) {
				this.templateData.morningTemplateId = obj.morningTemplateId ? obj.morningTemplateId : ''
				this.templateData.lianaitime = obj.lianaitime ? obj.lianaitime : ''
				this.templateData.birthday = obj.birthday ? obj.birthday : ''
				this.templateData.district = obj.district ? obj.district : ''
				this.templateData.remark = obj.remark ? obj.remark : ''
				this.templateData.ipAddress = obj.ipAddress ? obj.ipAddress : ''
				this.templateData.cronMoring = obj.cronmoring ? formatTime(obj.cronmoring) : ''
				this.templateData.cronNight = obj.cornnight ? formatTime(obj.cornnight) : ''
				this.templateData.nightStatus = obj.nightStatus
				this.templateData.moringStatus = obj.moringStatus
			},

			// 修改自定义参数
			updateTemplate(e) {
				if(!this.isValue){
					uni.navigateTo({
						url: '/pages/account/account?userInfo='+JSON.stringify(this.userInfo)
					})
				} else {
					this.$refs.form.validate().then(res => {
						uni.showLoading({
							title: '正在修改自定义参数'
						})
						let params = {
							appid: this.templateData.appid,
							secret: this.templateData.secret,
							openid: this.templateData.openid,
							// openid: 'o7Rzm6Kzd3QhwBoPaPVnky_htvQU',
							morningTemplateId: this.templateData.morningTemplateId,
							district: this.templateData.district,
							lianaitime: this.templateData.lianaitime,
							birthday: this.templateData.birthday,
							remark: this.templateData.remark,
							beizhu: 'beizhu',
							shengri: 'shengri',
							jinju: 'jinju',
							caihongpi: 'caihongpi',
							lianai: 'lianai',
							tianqi: 'tianqi',
							riqi: 'riqi',
							low: 'low',
							high: 'high',
							single: this.templateData.single,
							cronmoring: formatCorn(this.templateData.cronMoring),
							cornnight: formatCorn(this.templateData.cronNight),
							moringStatus: this.templateData.moringStatus,
							nightStatus: this.templateData.nightStatus,
						}
						setHasData(params).then(res => {
							uni.hideLoading()
							console.log(res);
							if (res.code === 200) {
								uni.showToast({
									title: `修改成功`,
									icon: 'success',
									duration: 1500
								})
							} else {
								uni.showToast({
									title: '设置失败',
									icon: 'error',
									duration: 1500
								})
							}
							setTimeout(()=>{
								// this.getTemplate()
								uni.navigateBack({
									delta:1
								})
							},1500)
						}).catch(() => {
							uni.hideLoading()
							uni.showToast({
								title: '修改失败',
								icon: 'error',
								duration: 1500
							})
						})
					})
				}
				
			},
			// 获取用户模板
			getTemplate() {
				uni.showLoading({
					title: '获取模板中'
				})
				getTemplate({
					openid: this.userInfo.openid
					// openid: 'oIrTY6Qzf2d_0ocTJkURActR0TxU'
				}).then(res => {
					if (res.code === 200) {
						if(res.data.district){
							this.template = res.data
							this.setData(res.data)
							this.isValue = true
						}else{
							this.isValue = false
						}
					} else{
						this.isValue = false
					}
					uni.hideLoading()
				}).catch(()=>{
					this.isValue = false
					uni.hideLoading()
				})
			},
			// 早上定时器修改
			timerMoring() {
				uni.showLoading({
					title: '正在修改早上定时器'
				})
				let params = {
					cron: {
						openid: this.templateData.openid,
						moringStatus: this.templateData.moringStatus,
					}
				}
				setCronMoringStatus(params).then(res => {
					uni.hideLoading()
					if (res.code === 200) {
						uni.showToast({
							title: `修改成功`,
							icon: 'success',
							duration: 1500
						})
					} else {
						this.templateData.moringStatus = this.template.moringStatus
						this.templateData.cronMoring = formatTime(this.template.cronmoring)
						uni.showToast({
							title: '设置失败',
							icon: 'error',
							duration: 1500
						})
					}
				}).catch(() => {
					this.templateData.moringStatus = this.template.moringStatus
					this.templateData.cronMoring = formatTime(this.template.cronmoring)
					uni.hideLoading()
					uni.showToast({
						title: '设置失败',
						icon: 'error',
						duration: 1500
					})
				})
			},
			// 晚上定时器修改
			timerNight() {
				uni.showLoading({
					title: '正在修改晚上定时器'
				})
				let params = {
					cron: {
						openid: this.templateData.openid,
						nightStatus: this.templateData.nightStatus,
					}
				}
				setCronMoringStatus(params).then(res => {
					uni.hideLoading()
					if (res.code === 200) {
						uni.showToast({
							title: `修改成功`,
							icon: 'success',
							duration: 1500
						})
					} else {
						this.templateData.nightStatus = this.template.nightStatus
						this.templateData.cronNight = formatTime(this.template.cornnight)
						uni.showToast({
							title: '设置失败',
							icon: 'error',
							duration: 1500
						})
					}
				}).catch(() => {
					this.templateData.nightStatus = this.template.nightStatus
					this.templateData.cronNight = formatTime(this.template.cornnight)
					uni.hideLoading()
					uni.showToast({
						title: '设置失败',
						icon: 'error',
						duration: 1500
					})
				})
			},
			// 滑块change事件
			changeSwitch(e, type) {
				if (type === 1) {
					this.templateData.moringStatus = e.detail.value ? 1 : 0
				} else {
					this.templateData.nightStatus = e.detail.value ? 1 : 0
				}

			},
			// 选择日式时间事件
			changeLog(e, type) {
				type === 1 ? this.templateData.cronMoring = e.detail.value : this.templateData.cronNight = e.detail.value
				// this.isTips = false
			},
		}
	}
</script>

<style lang="less" scoped>
	page {
		background-color: #fff;
	}

	.example {
		padding: 1rem;
	}

	// .btn_box {
	// 	display: flex;
	// 	justify-content: center;
	// }

	.pop_body {
		padding-bottom: 3rem;
	}

	.form_box {
		padding: 0 1rem
	}

	.line {
		@include flex;
		align-items: center;
		justify-content: center;
		padding: 1rem;
	}

	.line .label {
		// width: 150px;
		margin-right: 1rem;
	}

	.timer_box {
		display: flex;
		align-items: center;
	}

	.timer_box uni-button {
		padding-left: 0;
		padding-right: 0;
		width: 4rem;
		height: 2rem;
		font-size: .8rem;
		text-align: center;
		background-color: #007aff;
	}

	.uni-input {
		transition: .5s all;
		position: relative;
		font-size: 14px;
		height: 35px;
		line-height: 35px;
		box-sizing: border-box;
		border-radius: 4px;
		border: 1px solid #e5e5e5;
		padding: 0 10px;
		width: 13rem;
		margin-right: 1rem;
	}
</style>
